package com.china08.yxyapi.api.manage.tec.teaching.resource.myCollect;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.entity.mg.user.YxyUserFavor;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.repository.mg.YxyUserFavorRepository;
import com.china08.yxyapi.service.yxyresource.YxyResourceService;
import com.china08.yxyapi.util.EsResourcePvUpdateUtil;
import com.china08.yxyapi.util.EsResourceUpdateUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 老师 我的收藏 新增
 * 
 */
@CrossOrigin
@RestController
public class MyCollectCrtController extends YxyApiBaseController {
    @Autowired
    private YxyUserFavorRepository favorRepository;

    @Autowired
    private YxyResResourceRepository resourceRepository;

    @Autowired
    private YxyResourceService yrs;

    @ApiOperation(value = "老师收藏资源")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/resource/myCollect/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel,
            RespModel respModel) {
        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        Assert.notNull(StringUtils.defaultIfBlank(schoolId, null), "请关联学校");
        String role = getProfile(request, response).getRoleId();
        Assert.isTrue(StringUtils.equals("ROLE_TEACHER", role), "您当前不是老师身份");
        YxyUserFavor favor = favorRepository.findByResIdAndUserId(reqModel.resId, userId);
        if (favor == null) {
            favor = new YxyUserFavor();
            favor.setUserId(userId);
            favor.setResId(reqModel.resId);
            ElasticYxyResResource elasticYxyResResource = yrs.findById(reqModel.resId);
            Assert.notNull(elasticYxyResResource, "发生错误，请刷新页面");
            if (elasticYxyResResource.getTypeId().equals("yx14")) {
                favor.setResType("01");
            } else {
                favor.setResType("00");
            }
            YxyResResource resource = resourceRepository.findById(reqModel.resId);
            Assert.notNull(resource, "发生错误，请刷新页面");
            if (CollectionUtils.isNotEmpty(elasticYxyResResource.getPhaseIdList())) {
                favor.setPhase(elasticYxyResResource.getPhaseIdList().get(0));
                favor.setSubject(elasticYxyResResource.getSubjectIdList().get(0));
                favor.setVersionId(elasticYxyResResource.getVersionIdList().get(0));
                favor.setSemesterId(elasticYxyResResource.getSemesterIdList().get(0));
                if (CollectionUtils.isNotEmpty(elasticYxyResResource.getBookContentIdList())) {
                    favor.setBookContentId(elasticYxyResResource.getBookContentIdList().get(0));
                } else {
                    favor.setBookContentId(elasticYxyResResource.getBookContentId());
                }
            } else {
                favor.setPhase(elasticYxyResResource.getPhaseId());
                favor.setSubject(elasticYxyResResource.getSubjectId());
                favor.setVersionId(elasticYxyResResource.getVersionId());
                favor.setSemesterId(elasticYxyResResource.getSemesterId());
                if (CollectionUtils.isNotEmpty(elasticYxyResResource.getBookContentIdList())) {
                    favor.setBookContentId(elasticYxyResResource.getBookContentIdList().get(0));
                } else {
                    favor.setBookContentId(elasticYxyResResource.getBookContentId());
                }
            }
            favor.setResName(elasticYxyResResource.getTitle());
            favorRepository.save(favor);
            resource.setFavoriteCount(resource.getFavoriteCount() + 1);
            resourceRepository.save(resource);
            EsResourceUpdateUtil.updateEsResoure(reqModel.resId);
            EsResourcePvUpdateUtil.updateEsResoure(reqModel.resId + "_3");
        }
        respModel.setMessage("收藏成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String resId;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(resId, null), "请选择要收藏的资源");
        }

    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
